#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QValidator>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    for (int i=1;i<=4;i++)
        for(int j=1;j<=6;j++)
        {
            my[i][j] = new Mypushbutton(this);
            my[i][j]->setGeometry(j*80+50,i*80,80,80);
            my[i][j]->setText(QString::number(i)+QString::number(j));

            my[i][j]->p.x=i;
            my[i][j]->p.y=j;
            my[i][j]->setText(QString::number(my[i][j]->p.x)
                    +QString::number(my[i][j]->p.y));

            QIcon* icon = new QIcon(":/4.png");//填写当前图标的位置
            QIcon* icon6 = new QIcon(":/6.png");

//            my[i][j]->setIcon(*icon);
            if(i<=1)
                my[i][j]->setIcon(*icon);
            if(i>=2)
                my[i][j]->setIcon(*icon6);
            my[i][j]->setIconSize(QSize(40,40));

            connect(my[i][j],SIGNAL(clicked()),this,SLOT(on_mybutton_clicked()));
        }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_mybutton_clicked()
{
    qDebug()<<"***********on_mybutton_clicked****************";
    //获取当前点击的按钮对象
    Mypushbutton* btn=qobject_cast<Mypushbutton*>(sender());
    qDebug()<<btn->text();
    count++;
    if(count==1)
    {
        p1=btn;
        qDebug()<<p1<<"count="<<count;
    }
    if(count==2)
    {
        p2=btn;
        //点击了相同的按钮，清空
        if(p1==p2)
        {
            qDebug()<<p1->text()<<p2->text()<<count;
            p1=nullptr;p2=nullptr;count=0;
        }else {
            //点击了不同的按钮，就隐藏重新计数
//            p1->hide();p2->hide();
            if(mydata.canRemove(p1,p2))
            {
                qDebug<<"***************can remove"<<p1;
                mydata.myremove(p1,p2);
            }
            qDebug()<<p1->text()<<p2->text()<<count;
            p1=nullptr;p2=nullptr;count=0;
        }
    }
}
